package com.jn.moduledemo;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.jn.moduledemo.common.base.BaseActivity;
import com.jn.moduledemo.common.permission.PermissionHelper;
import com.jn.moduledemo.financing.FinancingActivity;
import com.jn.moduledemo.login.LoginActivity;


@Route(path = "/app/MainActivity")
public class MainActivity extends BaseActivity implements View.OnClickListener {

    private TextView mText;
    private PermissionHelper mPermissionHelper;
    private CountDownTimer mCountDownTimer;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void init() {
        //showToast("init");
        mText = findViewById(R.id.text);
        mText.setOnClickListener(this);
        mPermissionHelper = new PermissionHelper(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.SEND_SMS)
                != PackageManager.PERMISSION_GRANTED) {
            requestPermission();
        } else {
            mCountDownTimer = new CountDownTimer(3000, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    long s = (millisUntilFinished) / 1000;
                    String all = getString(R.string.main_timer, s + "");
                    mText.setText(all);
                }

                @Override
                public void onFinish() {
                    toLogin();
                }
            };
            mCountDownTimer.start();
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.text) {
            toLogin();
        }
    }

    private void requestPermission() {
        mPermissionHelper.requestPermission(
                new String[]{android.Manifest.permission.SEND_SMS,
                        android.Manifest.permission.READ_SMS,
                        android.Manifest.permission.READ_CONTACTS,
                        android.Manifest.permission.WRITE_CONTACTS,
                        Manifest.permission.CAMERA},
                1);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1) {
            toLogin();
        }
    }

    private void toLogin() {
        if (mCountDownTimer != null) {
            mCountDownTimer.cancel();
            mCountDownTimer = null;
        }
//        Intent intent = new Intent(MainActivity.this, FinancingActivity.class);
//        startActivity(intent);
//        finish();
        ARouter.getInstance().build("/financing/FinancingActivity").navigation();
        finish();
    }
}